<template>
  <el-breadcrumb class="app-breadcrumb" :class="sidebar.opened ? 'open' : 'notOpen' " separator="/">
    <el-button size="mini" class="el-icon-arrow-left backBtn" @click="historyBack"></el-button>
    <transition-group name="breadcrumb">
      <el-breadcrumb-item v-for="(item,index)  in levelList" :key="item.path" v-if="item.title">
        <span v-if="index==levelList.length-1" class="no-redirect">{{item.title}}</span>
        <router-link v-else :to="item.path">{{item.title}}</router-link>
      </el-breadcrumb-item>
    </transition-group>
  </el-breadcrumb>
</template>

<script>
  import { mapGetters } from 'vuex'
export default {
  created() {
    this.getBreadcrumb()
  },
  data() {
    return {
      levelList: null,
      specialRouter:[
        {routerName:'/datapush/detail',arr:[{title:'转化推送',path:'/datapush/index'},{title:'转化数据',path:'/datapush/detai'}]},
        {routerName:'/datapush/detail-t',arr:[{title:'批次数据列表',path:'/pushed/pushedlist'},{title:'转化数据',path:'/datapush/detai'}]},
        {routerName:'/datapush/detail-s',arr:[{title:'数据反查',path:'/datapush/datasearch'},{title:'转化数据',path:'/datapush/detai'}]},
      ]
    }
  },
  computed: {
    ...mapGetters([
      'sidebar'
    ])
  },
  watch: {
    $route() {
      this.getBreadcrumb()
    }
  },
  methods: {
    getBreadcrumb() {
      let matched = this.$route.matched.filter(item => item.name );
      this.levelList = this.getMatchedList(matched);
    },
    getMatchedList(matchedArr){
      var arr = [];
      var tempPath = this.$route.path;
      if(tempPath == '/datapush/detail' || tempPath == '/datapush/detail-t' || tempPath == '/datapush/detail-s'){
          arr = this.specialRouter.filter(v=>{ return v.routerName == tempPath})[0].arr;
      }else{
        matchedArr.forEach(v=>{
          if(v.parent.meta.title){
            if(!arr.some(n=> n.title === v.parent.meta.title)){
              arr.push(v.parent.meta)
            }
          }
          if(v.meta.title){
            if(!arr.some(n=> n.title === v.meta.title)){
              arr.push(v.meta)
            }
          }
        })
      }
      return arr
    },
    historyBack(){
      window.history.back()
    }
  }
}
</script>

<style type="text/scss" lang="scss" scoped>
  .app-breadcrumb.el-breadcrumb {
    display: inline-block;
    font-size: 14px;
    line-height: 50px;
    padding-left: 10px;
    border-bottom: solid 1px #e6e6e6;
    width: 100%;
    position: fixed;
    top: 50px;
    z-index: 1000;
    background-color: #fff;


    .no-redirect {
      color: #97a8be;
      cursor: text;
    }
    .backBtn {
      padding: 5px;
      margin-right: 12px;
    }
  }
  .open {
    width:100%;
    -moz-transition:all .28s; /* Firefox 4 */
    -webkit-transition:all .28s; /* Safari and Chrome */
    -o-transition:all .28s; /* Opera */
    transition:all .28s;
    left:180px;
  }
  .notOpen {
    width:100%;
    -moz-transition:all .28s; /* Firefox 4 */
    -webkit-transition:all .28s; /* Safari and Chrome */
    -o-transition:all .28s; /* Opera */
    transition:all .28s;
    left:36px;
  }
</style>
